package main

import (
	"fmt"
	"io"
	"os"
	"runtime"
)

// 2.获取当前go文件路径
// GetCurrentFilePath 获取当前文件(调用函数)路径
func GetCurrentFilePath() string {
	_, file, _, _ := runtime.Caller(1)
	return file
}

var file = "14.文件读取/1.go"

func main() {
	//1.一次性读取
	bytedata, _ := os.ReadFile(file)
	fmt.Println(string(bytedata))
	fmt.Println(GetCurrentFilePath())

	for i := 0; ; i++ {
		a, b, c, d := runtime.Caller(i)
		if !d {
			break
		}
		fmt.Println(a, b, c, d)
	}

	//3.分片读
	file, _ := os.Open(file)
	defer file.Close()
	for {
		buf := make([]byte, 10) //每次读10个字节
		_, err := file.Read(buf)
		if err == io.EOF {
			break
		}
		fmt.Printf("%s\n", buf)
	}
	//✅按行读 bufio.NewReader(file)
	//✅自定义分隔符读 bufio.NewScanner(file)
	//⚠️scanner.Split(bufio.ScanWords) // 按照单词读
	//⚠️scanner.Split(bufio.ScanLines) // 按照行读
	//⚠️scanner.Split(bufio.ScanRunes) // 按照中文字符读
	//⚠️scanner.Split(bufio.ScanBytes) // 按照字节读读，中文会乱码
	//🔵url:https://docs.fengfengzhidao.com/#/docs/%E6%96%B0golang%E5%9F%BA%E7%A1%80/16.%20%E6%96%87%E4%BB%B6%E6%93%8D%E4%BD%9C?id=%e5%88%86%e7%89%87%e8%af%bb
}
